using System;
using System.Collections.Generic;
using MultiplayerServerConnectionHandlers;
using UnityEngine;

public class CallibrationGameLayer : MonoBehaviour
{
	[Serializable]
	public class Callibration
	{
		public string instructions;

		public RacketCallibrationDB.ReferencePointDefinition.PositionType type;
	}

	public UILabel instructionsText;

	public List<RacketControlDataReceiver> rackets = new List<RacketControlDataReceiver>();

	public List<Callibration> callibrations = new List<Callibration>();

	private int activeCallibrationIndex;

	private void OnEnable()
	{
		ServerDetectionNavigation serverDetectionNavigation = NavigationManager.instance as ServerDetectionNavigation;
		if (serverDetectionNavigation != null)
		{
			serverDetectionNavigation.EnableView(ServerDetectionNavigation.CameraType.RacketView);
		}
		int num = 0;
		foreach (MessageHandler handler in BehaviourSingleton<GGUDPNetworkServer>.instance.handlers)
		{
			ConnectionHandler connectionHandler = handler as ConnectionHandler;
			if (connectionHandler != null)
			{
				num++;
				if (num > rackets.Count)
				{
					break;
				}
				rackets[num - 1].gameObject.SetActive(true);
				rackets[num - 1].myHandler = connectionHandler;
			}
		}
		activeCallibrationIndex = 0;
		ShowActiveCallibration();
	}

	private void OnDisable()
	{
		foreach (RacketControlDataReceiver racket in rackets)
		{
			if (racket != null && racket.gameObject != null)
			{
				racket.gameObject.SetActive(false);
			}
		}
	}

	private void ShowActiveCallibration()
	{
		if (activeCallibrationIndex >= callibrations.Count)
		{
			UITools.ChangeText(instructionsText, "Congrats, your device is now callibrated");
			{
				foreach (RacketControlDataReceiver racket in rackets)
				{
					if (racket.gameObject.activeSelf)
					{
						racket.shouldPollForData = true;
					}
				}
				return;
			}
		}
		Callibration callibration = callibrations[activeCallibrationIndex];
		UITools.ChangeText(instructionsText, callibration.instructions);
	}

	private void OnCallibrationReceived(ControllerData data)
	{
		Debug.Log("CallibrationGameLayer - receiving callibration");
		Callibration callibration = callibrations[activeCallibrationIndex];
		foreach (RacketControlDataReceiver racket in rackets)
		{
			if (racket.gameObject.activeSelf && racket.myHandler != null && racket.myHandler.myId == data.receiverId)
			{
				racket.SetReferencePoint(callibration.type, data);
			}
		}
		foreach (RacketControlDataReceiver racket2 in rackets)
		{
			if (!racket2.HasCallibratedForType(callibration.type))
			{
				return;
			}
		}
		activeCallibrationIndex++;
		ShowActiveCallibration();
	}

	private void Update()
	{
		if (activeCallibrationIndex >= callibrations.Count)
		{
			return;
		}
		GGUDPNetworkServer instance = BehaviourSingleton<GGUDPNetworkServer>.instance;
		foreach (MessageHandler handler in instance.handlers)
		{
			ConnectionHandler connectionHandler = handler as ConnectionHandler;
			if (connectionHandler == null || !connectionHandler.isActive)
			{
				continue;
			}
			bool flag = false;
			for (int num = connectionHandler.unreadMessages.Count - 1; num >= 0; num--)
			{
				if (connectionHandler.unreadMessages[num].type == UDPMessageBase.MessageType.ControllerDataMessage)
				{
					ControllerData message = connectionHandler.unreadMessages[num].GetMessage<ControllerData>();
					if (!flag && message.isUserTouchingSurface)
					{
						flag = true;
						OnCallibrationReceived(message);
					}
					connectionHandler.unreadMessages.RemoveAt(num);
				}
			}
		}
	}
}
